#! /bin/sh
# 
# Laptop mode tools module: Ethernet power saving tweaks.
#

if [ x$CONTROL_ETHERNET = x1 ] ; then
	if [ $ON_AC -eq 1 ]; then
		if [ "$ACTIVATE" -eq 1 ]; then
			THROTTLE_ETHERNET="$LM_AC_THROTTLE_ETHERNET"
		else
			THROTTLE_ETHERNET="$NOLM_AC_THROTTLE_ETHERNET"
		fi

                if [ x$DISABLE_ETHERNET_ON_BATTERY = x1 ]; then
                        # We are ON_AC and Disable feature is requested
                        # So we might be required to re-enable the device.
                        DISABLE_ETHERNET=0 #zero would mean that the device needs be re-enabled.
                else
                        DISABLE_ETHERNET=2
                        # IF this is the case, don't touch the device for
                        # enable/disable
                fi
	else
		THROTTLE_ETHERNET="$BATT_THROTTLE_ETHERNET"

                if [ x$DISABLE_ETHERNET_ON_BATTERY = x1 ]; then
                        DISABLE_ETHERNET=1
                else
                        DISABLE_ETHERNET=0
                fi
	fi
	
	for DEVICE in $ETHERNET_DEVICES ; do
                DISABLED=0
                path=`readlink -f /sys/class/net/$DEVICE`
                log "VERBOSE" "ethernet: $path"
                if ! [ -z $path ]; then
                        if [ -d $path/device ]; then
                          dev_path=`readlink -f $path/device`
                          log "VERBOSE" "ethernet: $dev_path"
                        fi
                fi

                if ! [ -z $dev_path ] && [ -f $dev_path/enable ]; then
                        if [ x$DISABLE_ETHERNET = x1 ]; then
                                if [ -f $dev_path/enable ]; then
                                        echo 0 > $dev_path/enable
                                        log "VERBOSE" "ethernet: Disabling ethernet device $DEVICE"
                                        DISABLED=1
                                fi
                        elif [ x$DISABLE_ETHERNET = x0 ]; then
                                if [ -f $dev_path/enable ]; then
                                        echo 1 > $dev_path/enable
                                        log "VERBOSE" "ethernet: Re-enabling ethernet device $DEVICE"
                                        DISABLED=1
                                fi
                        elif [ x$DISABLE_ETHERNET = x2 ]; then
                                DISABLED=0 # Be safe. :-)
                        else
                                DISABLED=0 # Same here. Be safe. :-)
                                           # For all other cases also, just disable it.
                        fi
                else
                        log "VERBOSE" "$DEVICE does not seem to be supporting enable/disable"
                fi

               if [ x$DISABLED = x1 ]; then
                       continue
               fi

		# Wakeup-on-LAN handling
		if [ x$DISABLE_WAKEUP_ON_LAN = x1 ] ; then
			ret=`ethtool -s $DEVICE wol d 2>&1`
                        exit_status=$?;
			log "VERBOSE" "$ret"
			if [ $exit_status -eq 0 ]; then			
				log "VERBOSE" "Enabled wakeup-on-LAN for $DEVICE"
			else
				log "VERBOSE" "Could not enable wakeup-on-LAN for $DEVICE"
			fi
		fi
		
		# Determine speed
		speed=`mii-tool -v $DEVICE | grep capabilities | tr ' ' '\n' |\
                        sort -n | sed -ne '/^1.*/p' | cut -d "b" -f1`
                if [ -z "$speed" ]; then
                        speed=0;
                fi
		max_s=0;
		min_s=100000;
		for s in $speed;
		do
			if [ $s -gt $max_s ]; then
				max_s=$s
			fi
			if [ $s -lt $min_s ]; then
				min_s=$s
			fi
		done
		MAX_SPEED=$max_s;

		case "$THROTTLE_SPEED" in
			"slowest")
			THROTTLE_SPEED=$min_s
			;;
			"fastest")
			THROTTLE_SPEED=$max_s
			;;
		esac

		# Handle throttling
		if [ x$THROTTLE_ETHERNET = x1 ] ; then
			# Handle auto-negotiation
			ret=`ethtool -s $DEVICE autoneg off 2>&1`
                        exit_status=$?;
                        log "VERBOSE" "$ret";
			if [ $exit_status -eq 0 ]; then
				log "VERBOSE" "Switched auto-negotiation to off for $DEVICE"
			else
				log "VERBOSE" "Could not set auto-negotiation to off for $DEVICE"
			fi

			# Handle Speed Throttling
			ret=`ethtool -s $DEVICE speed $THROTTLE_SPEED 2>&1`
                        exit_status=$?;
                        log "VERBOSE" "$ret";
			if [ $exit_status -eq 0 ]; then
				log "VERBOSE" "Throttled speed to $THROTTLE_SPEED Mbit for $DEVICE"
			else
				log "VERBOSE" "Could not throttle speed for $DEVICE"
			fi
		else
			# Handle auto-negotiation
			ret=`ethtool -s $DEVICE autoneg on 2>&1`
                        exit_status=$?;
                        log "VERBOSE" "$ret";
			if [ $exit_status -eq 0 ]; then
				log "VERBOSE" "Switched auto-negotiation to on for $DEVICE"
			else
				log "VERBOSE" "Could not set auto-negotiation to on for $DEVICE"
			fi

			# Handle Speed Throttling
			ret=`ethtool -s $DEVICE speed $MAX_SPEED 2>&1`
                        exit_status=$?;
                        log "VERBOSE" "$ret";
			if [ $exit_status -eq 0 ]; then
				log "VERBOSE" "Restored speed to $MAX_SPEED Mbit for $DEVICE"
			else
				log "VERBOSE" "Could not restore speed for $DEVICE"
			fi
		fi
	done
else
	log "VERBOSE" "Ethernet module is disabled."
fi

